package com.gnerv.cms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gnerv.cms.dto.TaskDTO;
import com.gnerv.cms.dto.TaskSaveDTO;
import com.gnerv.cms.dto.TaskSearchDTO;
import com.gnerv.cms.entity.Task;
import com.gnerv.cms.mapper.TaskMapper;
import com.gnerv.cms.service.IJobDetailService;
import com.gnerv.cms.service.ITaskService;
import com.gnerv.cms.service.ITriggerService;
import com.gnerv.cms.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {

    @Resource
    Scheduler scheduler;

    @Resource
    IJobDetailService jobDetailService;

    @Resource
    ITriggerService triggerService;

    @Resource
    TaskMapper taskMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveTask(TaskSaveDTO taskSaveDTO) {
        Integer total = taskSaveDTO.getTotal();
        total = (total >= Integer.MAX_VALUE) ? Integer.MAX_VALUE - 1 : total;
        if(StringUtils.isEmpty(taskSaveDTO.getUkId())){
            createTask(taskSaveDTO);
        }else {
            updateTask(taskSaveDTO);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createTask(TaskSaveDTO taskSaveDTO) {
        taskSaveDTO.setUkId(UUIDUtils.getLower());
        Task task = taskSaveDTO.convert();
        taskMapper.insert(task);
        JobDetail jobDetail = jobDetailService.createJobDetail(taskSaveDTO);
        Trigger trigger = triggerService.createTrigger(task);
        try {
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (SchedulerException e) {
            log.info("任务创建失败");
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteTask(String taskId) {
        triggerService.stopTrigger(taskId);
        triggerService.deleteTrigger(taskId);
        jobDetailService.deleteJobDetail(taskId);
        taskMapper.deleteById(taskId);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTask(TaskSaveDTO taskSaveDTO) {
        Task task = taskSaveDTO.convert();
        taskMapper.updateById(task);

        triggerService.stopTrigger(task.getUkId());
        triggerService.deleteTrigger(task.getUkId());
        jobDetailService.deleteJobDetail(task.getUkId());

        JobDetail jobDetail = jobDetailService.createJobDetail(taskSaveDTO);
        Trigger trigger = triggerService.createTrigger(task);
        try {
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (SchedulerException e) {
            log.info("任务更新失败");
        }
        return true;
    }

    @Override
    public TaskDTO selectTask(String taskId) {
        TaskDTO task = taskMapper.selectTask(taskId);
        return task;
    }

    @Override
    public List<TaskDTO> listTask(TaskSearchDTO taskSearchDTO) {
        List<TaskDTO> tasks = taskMapper.listTask(taskSearchDTO);
        return tasks;
    }

    @Override
    public boolean startTask(String taskId) {
        triggerService.startTrigger(taskId);
        taskMapper.updateTaskStatus(1, taskId);
        return true;
    }

    @Override
    public boolean stopTask(String taskId) {
        triggerService.stopTrigger(taskId);
        taskMapper.updateTaskStatus(0, taskId);
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateTaskCount(String taskId) {
        taskMapper.updateTaskCount(taskId);
        return true;
    }


}
